<template>
  <div class="home-container">
    <el-container class="responsive-layout">
      <el-header class="responsive-header">
        <nav class="nav-menu">
          <div class="logo">
            <img src="../assets/wow-logo.png" alt="WoW Logo" />
          </div>
          <div class="nav-links">
            <el-button type="primary" @click="showRegister = true">注册</el-button>
            <el-button @click="showLogin = true">登录</el-button>
          </div>
        </nav>
      </el-header>
      
      <el-main class="responsive-main">
        <div class="hero-section">
          <h1 class="responsive-title">欢迎来到魔兽世界</h1>
          <p class="responsive-text">加入冒险，创建你的账号！</p>
          <el-button type="primary" size="large" @click="showRegister = true">
            立即注册
          </el-button>
        </div>
        
        <div class="features-section">
          <div class="responsive-grid">
            <el-card class="feature-card">
              <template #header>
                <h3 class="responsive-heading">创建你的角色</h3>
              </template>
              <p class="responsive-text">选择你的种族和职业，开启你的冒险之旅。</p>
            </el-card>
            
            <el-card class="feature-card">
              <template #header>
                <h3 class="responsive-heading">加入战斗</h3>
              </template>
              <p class="responsive-text">体验史诗级的PVP战斗和挑战性的副本。</p>
            </el-card>
            
            <el-card class="feature-card">
              <template #header>
                <h3 class="responsive-heading">探索艾泽拉斯</h3>
              </template>
              <p class="responsive-text">探索广阔的土地，完成精彩的任务。</p>
            </el-card>
          </div>
        </div>
      </el-main>
      
      <el-footer class="responsive-footer">
        <p>&copy; 2024 魔兽世界. 保留所有权利。</p>
      </el-footer>
    </el-container>

    <!-- Login Dialog -->
    <el-dialog
      v-model="showLogin"
      title="登录"
      width="400px"
      :close-on-click-modal="false"
      class="login-dialog"
    >
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading" class="submit-btn">
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="register-link">
        还没有账号？ <el-button type="text" @click="switchToRegister">立即注册</el-button>
      </div>
    </el-dialog>

    <!-- Register Dialog -->
    <el-dialog
      v-model="showRegister"
      title="创建账号"
      width="400px"
      :close-on-click-modal="false"
      class="register-dialog"
    >
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" type="email" placeholder="请输入邮箱" />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleRegister" :loading="loading" class="submit-btn">
            注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-link">
        已有账号？ <el-button type="text" @click="switchToLogin">立即登录</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { ElMessage } from 'element-plus'
import { Menu } from '@element-plus/icons-vue'

const router = useRouter()
const authStore = useAuthStore()
const mobileMenuOpen = ref(false)
const showLogin = ref(false)
const showRegister = ref(false)
const loading = ref(false)
const isMobile = ref(window.innerWidth < 768)
const loginFormRef = ref()
const registerFormRef = ref()

const loginForm = reactive({
  username: '',
  password: ''
})

const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度应在3到20之间', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱', trigger: 'blur' }
  ],
  password: [
    { required: true, message: 'Please enter your password', trigger: 'blur' },
    { min: 6, message: 'Password must be at least 6 characters', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: 'Please confirm your password', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: Function) => {
        if (value !== registerForm.password) {
          callback(new Error('Passwords do not match'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value
}

const closeMobileMenu = () => {
  mobileMenuOpen.value = false
}

const switchToRegister = () => {
  showLogin.value = false
  showRegister.value = true
}

const switchToLogin = () => {
  showRegister.value = false
  showLogin.value = true
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      try {
        const success = await authStore.login(loginForm.username, loginForm.password)
        
        if (success) {
          ElMessage.success('Login successful!')
          showLogin.value = false
          router.push('/account')
        } else {
          ElMessage.error('Invalid username or password')
        }
      } catch (error) {
        ElMessage.error('An error occurred during login')
      } finally {
        loading.value = false
      }
    }
  })
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      try {
        const success = await authStore.register({
          username: registerForm.username,
          email: registerForm.email,
          password: registerForm.password
        })
        
        if (success) {
          ElMessage.success('Registration successful!')
          showRegister.value = false
          router.push('/account')
        } else {
          ElMessage.error('Registration failed. Please try again.')
        }
      } catch (error) {
        ElMessage.error('An error occurred during registration')
      } finally {
        loading.value = false
      }
    }
  })
}

// 监听窗口大小变化
const handleResize = () => {
  isMobile.value = window.innerWidth < 768
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, var(--background-dark) 0%, var(--background-darker) 100%);
  color: var(--text-color);
}

.responsive-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.responsive-main {
  flex: 1;
  padding: 2rem;
}

.responsive-footer {
  text-align: center;
  padding: 1.5rem;
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border-color);
  margin-top: auto;
}

.responsive-header {
  padding: 0;
  height: auto;
  width: 100%;
  background: rgba(9, 21, 33, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-color);
}

.nav-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  width: 100%;
}

.logo img {
  height: 40px;
  opacity: 0.9;
}

.nav-links {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.el-button--primary {
  background: #00aeff;
  border-color: #00aeff;
  color: #ffffff;
  font-weight: 500;
  transition: all 0.2s ease;
}

.el-button--primary:hover {
  background: #33beff;
  border-color: #33beff;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 174, 255, 0.3);
}

.el-button--primary:active {
  background: #0095db;
  border-color: #0095db;
  transform: translateY(0);
}

.el-button {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #ffffff;
  font-weight: 500;
  transition: all 0.2s ease;
}

.el-button:hover {
  background: rgba(0, 174, 255, 0.1);
  border-color: #00aeff;
  color: #00aeff;
  transform: translateY(-1px);
}

.el-button:active {
  background: rgba(0, 174, 255, 0.2);
  transform: translateY(0);
}

.hero-section {
  text-align: center;
  padding: 4rem 2rem;
  position: relative;
}

.hero-section h1 {
  font-size: 3rem;
  margin-bottom: 1.5rem;
  color: #ffffff;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.hero-section p {
  font-size: 1.2rem;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 2rem;
}

.features-section {
  padding: 4rem 2rem;
}

.feature-card {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  transition: transform 0.3s, box-shadow 0.3s;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(155, 77, 202, 0.2);
}

.el-card :deep(.el-card__header) {
  background: var(--card-header-bg);
  border-bottom: 1px solid var(--border-color);
  padding: 1.5rem;
}

.el-card :deep(.el-card__body) {
  padding: 1.5rem;
}

.responsive-heading {
  color: #ffffff;
  font-size: 1.5rem;
  margin: 0;
  font-weight: 600;
}

.responsive-text {
  color: rgba(255, 255, 255, 0.85);
  line-height: 1.6;
  margin: 0;
}

.responsive-footer p {
  color: rgba(255, 255, 255, 0.7);
  margin: 0;
}

:deep(.el-dialog) {
  background: var(--background-dark);
  border: 1px solid var(--border-color);
  border-radius: 8px;
}

:deep(.el-dialog__header) {
  padding: 20px;
  margin: 0;
  border-bottom: 1px solid var(--border-color);
}

:deep(.el-dialog__title) {
  color: #ffffff;
  font-size: 1.5rem;
  font-weight: 600;
}

:deep(.el-dialog__body) {
  padding: 30px 20px;
}

:deep(.el-form-item__label) {
  color: rgba(255, 255, 255, 0.9);
  font-size: 1rem;
  margin-bottom: 8px;
}

:deep(.el-input__inner) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #ffffff;
  height: 40px;
  border-radius: 4px;
}

:deep(.el-input__inner:focus) {
  border-color: var(--primary-color);
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.5);
}

.submit-btn {
  width: 100%;
  height: 40px;
  font-size: 1rem;
  margin-top: 10px;
}

.register-link,
.login-link {
  text-align: center;
  margin-top: 20px;
  color: var(--text-secondary);
}

.register-link :deep(.el-button),
.login-link :deep(.el-button) {
  color: #ffffff;
  font-size: 1rem;
}

.register-link :deep(.el-button:hover),
.login-link :deep(.el-button:hover) {
  color: rgba(255, 255, 255, 0.8);
}

@media (max-width: 768px) {
  .nav-menu {
    padding: 1rem;
  }
  
  .logo img {
    height: 30px;
  }
  
  .nav-links {
    gap: 0.5rem;
  }
  
  .el-button {
    padding: 8px 15px;
    font-size: 14px;
  }
  
  .hero-section h1 {
    font-size: 2rem;
  }
  
  .hero-section p {
    font-size: 1rem;
  }
}
</style> 